{extend name="../../base/view/common/base" /}
{block name="style"}
<style>
.layui-form-label{width:120px}
.layui-input-block{margin-left:150px}
</style>
{/block}
{block name="body"}
<div class="layui-form p-page">
    <form class="layui-form" lay-filter="docForm">
        <input type="hidden" name="id" value="{$detail.id|default='0'}">
        <div class="layui-form-item">
            <label class="layui-form-label">关联案件：</label>
            <div class="layui-input-block">
                <input type="text" name="case_code" placeholder="请选择关联案件" readonly class="layui-input picker-case" data-type="1" autocomplete="off" />
                <input type="hidden" name="case_id" value="{$detail.case_id|default=''}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文档名称：</label>
            <div class="layui-input-block">
                <input type="text" name="title" class="layui-input" placeholder="请输入文档名称" value="{$detail.doc_name|default=''}" lay-verify="required" lay-reqText="请输入文档名称">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">文档类型：</label>
            <div class="layui-input-block">
                <select name="doc_type" lay-verify="required" lay-reqText="请选择文档类型">
                    <option value="1">起诉状</option>
                    <option value="2">委托代理材料</option>
                    <option value="3">原告身份证明</option>
                    <option value="4">证据目录及证据材料</option>
                    <option value="5">被告身份信息</option>
                    <option value="6">银行账户确认书</option>
                    <option value="7">送达地址确认书</option>
                    <option value="7">裁判文书/调解文书</option>
                    <option value="8">其他</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上传文件：</label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-sm" id="uploadBtn"><i class="layui-icon"></i>附件上传</button>
                <div class="layui-row" id="uploadBox">
                    <input type="hidden" id="fileBoxInput" data-type="file" name="file_ids" value="">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitForm" id="submitBtn">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
{/block}

{block name="script"}
<script>
    function gouguInit() {
    // 从URL获取参数并初始化表单
    var urlParams = new URLSearchParams(window.location.search);
    var case_id = urlParams.get('case_id');
    var doc_type = urlParams.get('doc_type');
    var case_code = urlParams.get('case_code');
    
    if(case_id) {
        $('input[name="case_id"]').val(case_id);
        if(case_code) {
            $('input[name="case_code"]').val(case_code);
        } else {
            // 这里需要根据case_id获取案件信息并填充case_code
        }
    }
    
    if(doc_type) {
        $('select[name="doc_type"]').val(doc_type);
        layui.form.render('select');
    }
        layui.use(['form', 'uploadPlus', 'layer', 'tool', 'oaPicker'], function() {
            var form = layui.form;
            var uploadPlus = layui.uploadPlus;
            var tool = layui.tool;
            var oaPicker = layui.oaPicker;
            
            //附件上传
            var attachment = new uploadPlus({
                "target":'uploadBtn',
                "targetBox":'uploadBox',
                "url":'/api/index/upload'
            });

            // 如果已有案件ID，则禁用案件选择器
if($('input[name="case_id"]').val()) {
    $('.picker-case').prop('disabled', true);
}

// 案件选择器
            $('.picker-case').on('click', function() {
                let opts = {
                    title: '选择案件',
                    url: '/lawcase/api/get_case_list',
                    area: ['600px', '580px'],
                    cols: [
                        {field: 'case_cause_name', title: '案由', width: 150, align:'center'},
                        {field: 'case_code', title: '案号', align:'center'},
                        {field: 'lawyer_name', title: '代理律师', align:'center'}
                    ],
                    valueField: 'id',
                    page: true,
                    search: true
                };
                
                oaPicker.picker(opts, 1, function(selectData){
                    if(selectData.length > 0) {
                        if (!selectData[0] || !selectData[0].id) {
                            layer.msg('请选择有效的案件');
                            return;
                        }
                        $('input[name="case_id"]').val(selectData[0].id);
                        $('input[name="case_code"]').val(selectData[0].case_code);
                        
                        let callback = function(e) {
                            layer.msg(e.msg);
                            if(e.code === 0) {
                                parent.layui.table.reload('docTable');
                            }
                        };
                    }
                });
            });
            
            // 表单提交
            form.on('submit(submitForm)', function(data) {
                if($('#fileBoxInput').val() === '') {
                    layer.msg('请至少上传一个文件', {icon: 2});
                    return false;
                }
                
                var callback = function (e) {
                    layer.msg(e.msg);
                    if (e.code == 0) {
                        parent.layui.table.reload('docTable');
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    } else {
                        layer.msg(e.msg || '操作失败', {icon: 2});
                    }
                }
                console.log('Submitting form data:', data.field); // 添加调试日志
                tool.post("/lawcase/casedoc/" + (data.field.id == 0 ? 'add' : 'edit'), data.field, callback, {
                    headers: {'Content-Type': 'application/json'},
                    dataType: 'json'
                });
                return false;
            });
        });
    };
</script>
{/block}